from TicTacToe import TicTacToe

def main():
    game = TicTacToe()
    print("井字棋游戏开始！")
    print("你使用X，AI使用O")
    print("输入位置 (0-8):")
    print("0 | 1 | 2")
    print("---------")
    print("3 | 4 | 5")
    print("---------")
    print("6 | 7 | 8")
    
    while True:
        game.print_board()
        
        # 玩家回合
        while True:
            try:
                move = int(input("请输入你的移动 (0-8): "))
                if 0 <= move <= 8:
                    row = move // 3
                    col = move % 3
                    if game.make_move((row, col), game.human):
                        break
                print("无效的移动，请重试！")
            except ValueError:
                print("请输入有效的数字！")
                
        # 检查游戏是否结束
        winner = game.check_winner()
        if winner is not None:
            game.print_board()
            if winner == 1:
                print("恭喜你赢了！")
            elif winner == 0:
                print("平局！")
            break
            
        # AI回合
        print("\nAI思考中...")
        ai_move = game.get_best_move()
        game.make_move(ai_move, game.ai)
        
        # 检查游戏是否结束
        winner = game.check_winner()
        if winner is not None:
            game.print_board()
            if winner == -1:
                print("AI赢了！")
            elif winner == 0:
                print("平局！")
            break

if __name__ == "__main__":
    main()